{ ETP Link minilong buy 170222 }
i1(
ppnat=or(ge(xtime(d,m),1),ge(xtime(date(),h),10))
lt1=lasttrade(b,d)
lt2=lasttrade(s,d)
minSedanKp=mult(sub(date(),lt1),1440)
minSedanSlj=mult(sub(date(),lt2),1440)
tid_ok=and(ppnat,and(gt(minSedanKp,0.33),gt(minSedanSlj,0.33)))
cell=scrpar(32)
insats=scrpar(21)
replikera=eqv(insats,0)
id_ok=and(ge(cell,10),le(cell,799))

sync1=and(and(eqv(xtime(date(),m),59),ge(xtime(date(),s),30)),le(xtime(date(),h),16))
sync2=and(and(eqv(xtime(date(),m),59),ge(xtime(date(),s),40)),le(xtime(date(),h),16))
reset=and(sync1,not(sync2))
setgvarif(0,add(cell,1000),reset)
setgvarif(10000,add(cell,2000),reset)
setgvarif(0,add(cell,3000),reset)
wanted_price=getgvar(add(cell,1000))
diff=abs(sub(odepth(s,p,0),wanted_price))
closer=lt(diff,getgvar(add(2000,cell)))
include=scrpar(30)
set_new=and(and(sync2,and(closer,wanted_price)),include)
setgvarif(diff,add(2000,cell),set_new)
setgvarif(crcid(),add(3000,cell),set_new)
best_match=getgvar(add(3000,cell))
buy_paper=or(not(include),or(and(eqv(best_match,crcid()),wanted_price),not(wanted_price)))

antal=if(and(buy_paper,gt(getgvar(cell),0)),getgvar(cell),0)
adjust1=lt(portfolio(v),antal)
adjust2=and(le(portfolio(v),0),gt(antal,0))
samma_dag=eqv(int(d),int(date()))
kp1=and(and(and(if(replikera,adjust1,adjust2),samma_dag),tid_ok),id_ok)
kp2=and(kp1,or(not(include),not(sync1)))
Mult(kp2,10)
)